package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.BR
import com.stars.empoly.model.CityList
import com.stars.empoly.model.FindApplyBean
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch

class CooperationVModel(application: Application) : BViewModel<BRepository>(application) {


    var sex:String="1"//默认男
    var cityId:String?=null
    var lifePhoto=MutableLiveData<String>()

    var cityList= MutableLiveData<List<CityList>>()

    var bean=MutableLiveData<FindApplyBean>()


    @Bindable
    open var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }


    @Bindable
    open var age: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.age)
        }



    @Bindable
    open var phone: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }


    @Bindable
    open var code: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.code)
        }

    fun toSubmit(){
        if(name.isNullOrEmpty()){
            toast("请输入姓名")
            return
        }
        if(age.isNullOrEmpty()){
            toast("请输入年龄")
            return
        }
        if(cityId.isNullOrEmpty()){
            toast("请选择意向城市")
            return
        }
        if(phone.isNullOrEmpty()){
            toast("请输入联系电话")
            return
        }
        if(lifePhoto.value.isNullOrEmpty()){
            toast("上传本人近期照片")
            return
        }

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.technicianApply(name,sex,age,cityId,phone,lifePhoto.value,code) }
            if(it.ok(true)){
                (repository().context as Activity).finish()
            }
        }



    }

    fun getCityList(){
        viewModelScope.launch{

            var it=repository().lreq { NetEngine.service.cityList() }

            if(it.ok()){
                cityList.value=it.data
            }
        }

    }

    fun getCoopStatus(){
        viewModelScope.launch{

            var it=repository().lreq { NetEngine.service.findApply() }

            if(it.ok()){
                bean.value=it.data
            }
        }
    }




}
